
package com.insigma.business.qrcode.common._consumer.impl;

import com.google.common.cache.Cache;
import com.insigma.business.qrcode.common.dto.CommonFavoriteSearchRequest;
import com.insigma.business.qrcode.common.dto.CommonFavoriteUpdateRequest;
import com.insigma.business.qrcode.common._consumer.CommonFavoriteConsumer;
import com.insigma.business.qrcode.common._provider.CommonFavoriteProvider;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.MemCacheManager;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.pay.dto.CommonFavoriteInsertRequest;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.welfare.dto.CommonFavoriteDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;


@Service("commonFavoriteConsumer")
public class CommonFavoriteConsumerImpl implements CommonFavoriteConsumer {

    @Autowired
    private CommonFavoriteProvider commonFavoriteProvider;

    Cache<String, Object> cache = MemCacheManager.getInstance(5, TimeUnit.MINUTES);
    
    @Override
    public BasePagingResponse<List<CommonFavoriteDTO>> searchCommonFavorite(final CommonFavoriteSearchRequest searchRequest) {
        return commonFavoriteProvider.searchCommonFavorite(searchRequest);
    }

   
    @Override
    public BaseResponse<CommonFavoriteDTO> getCommonFavorite(final BaseGetRequest baseGetRequest) {
        return commonFavoriteProvider.getCommonFavorite(baseGetRequest);
    }

    
    @Override
    public BaseResponse<String> insertCommonFavorite(final CommonFavoriteInsertRequest insertRequest) {
        return commonFavoriteProvider.insertCommonFavorite(insertRequest);
    }

    
    @Override
    public BaseResponse<Integer> updateCommonFavorite(final CommonFavoriteUpdateRequest updateRequest) {
        return commonFavoriteProvider.updateCommonFavorite(updateRequest);
    }

    
    @Override
    public BaseResponse<Integer> deleteCommonFavorite(final BaseDeleteRequest deleteRequest) {
        return commonFavoriteProvider.deleteCommonFavorite(deleteRequest);
    }

    @Override
    public BaseResponse<Boolean> cachedIsFavoriteP(String type, String bizId, String soldierId) {
       String key = "favorite:cache:" + type + ":" + bizId + ":" +soldierId;
        Object ifPresent = cache.getIfPresent(key);
        if (ifPresent == null) {
            Boolean dto = commonFavoriteProvider.cachedIsFavoriteP(type, bizId, soldierId).getDto();
            cache.put(key, dto);
            ifPresent = dto;
        }

        return new BaseResponse(true, (Boolean)ifPresent);
    }

}
